package com.lisynet.tools.mytools.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lisynet.tools.mytools.entity.User;
import com.lisynet.tools.mytools.entity.UserInfo;
import com.lisynet.tools.mytools.mapper.UserMapper;
import com.lisynet.tools.mytools.service.IUserInfoService;
import com.lisynet.tools.mytools.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lisy
 * @since 2022-01-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private IUserInfoService userInfoService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public User addUser(User u) {
        this.baseMapper.insert(u);
        Integer userId = u.getId();
        userInfoService.addUserInfo(UserInfo.builder()
                .userId(userId)
                .msg1(IdUtil.simpleUUID())
                .msg2(IdUtil.simpleUUID())
                .updateTime(LocalDateTime.now())
                .build());
        return this.getInfo(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public User changeInfo(Integer id) {
        User u = this.getInfo(id);
        u.setAge(RandomUtil.randomInt(20,99));
        this.baseMapper.updateById(u);
        return this.getInfo(id);
    }

    @Override
    public User getInfo(Integer id) {
        return this.baseMapper.selectById(id);
    }
    @PostConstruct
    public void setProperties(){
        System.setProperty("druid.mysql.usePingMethod","false");
    }
}
